<?php

namespace App\Http\Controllers;

use App\Models\Device;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Excel;
use Illuminate\Support\Facades\DB;

class ExcelController extends Controller
{
    public function exportExcel($data,$header,$filename,$type,$other){
        ini_set('memory_limit','1024M');
        iconv('UTF-8', 'GB2312//IGNORE', $filename);
        $print = [$header];
        if($type == 'jiafen'){
            $sum1 = $sum2 = $sum3 = $sum4 = $sum5 = $sum6 = $sum7 = $sum8 = 0;
            foreach ($data as $row){
                $row['deviceGroup'] = $row['deviceGroup'] ? $row['deviceGroup']->name : '';
                $sum1 += $row['totalWechatRequest'];
                $sum2 += $row['todayWechatRequest'];
                $sum3 += $row['wechat_friend'];
                $sum4 += $row['totalAlipayRequest'];
                $sum5 += $row['sendCardNum'];
                $sum6 += $row['deletFriendsNum'];
                $sum7 += $row['addNum'];
                $sum8 += $row['foster'];
                $print[] = [
                    $row['id'],$row['deviceGroup'],$row['imei'],$row['phone_number'],
                    $row['wechat'],$row['totalWechatRequest'],$row['todayWechatRequest'],
                    $row['wechat_friend'],$row['totalAlipayRequest'],$row['sendCardNum'],
                    $row['deletFriendsNum'],$row['addNum'],$row['foster']
                ];
            }
            $print[] = ['','','','','Total',$sum1,$sum2,$sum3,$sum4,$sum5,$sum6,$sum7,$sum8];
        }
        Excel::create($filename,function($excel) use ($print){
            $excel->sheet('sheet', function($sheet) use ($print) {
                $sheet->rows($print);
            });
        })->export('xlsx');
    }

    public function jiafenExcel(Request $request){
        $created_at = $request->created_at ? Carbon::parse($request->created_at) : Carbon::today();
        $filename = explode(' ',$created_at);
        $filename = $filename[0];
        $query = Device::with([
            'deviceDetailed'=> function ($query) use($created_at) {
                $query->where(DB::raw('date(created_at)'), $created_at);
            },
            'posts' => function ($query) use($created_at) {
                $query->where(DB::raw('date(created_at)'), $created_at);
            },
            'deviceGroup'
        ]);
        if($request->device_group_id > 0) {
            $query->where('device_group_id', $request->device_group_id);
            $filename .= '[groupId:'.$request->device_group_id.']';
        }
        if($request->device_code){
            $query->where('device_code','like','%'. $request->device_code .'%');
            $filename .= '[search:'.$request->imei.']';
        }
        $devices = $query->get();
        foreach ($devices as $row){
            $row->wechat_friend = $row->totalWechatRequest = $row->todayWechatRequest
                = $row->totalAlipayRequest = $row->sendCardNum = $row->deletFriendsNum
                = $row->addNum = $row->foster = 0;
            if($row->deviceDetailed){
                foreach ($row->deviceDetailed as $detail){
                    if($detail->detail_type == 10)
                        $row->wechat_friend = $detail->detail_count;
                    if($detail->detail_type == 12)
                        $row->sendCardNum = $detail->detail_count;
                    if($detail->detail_type == 13)
                        $row->deletFriendsNum = $detail->detail_count;
                    if($detail->detail_type == 14)
                        $row->addNum = $detail->detail_count;
                    if($detail->detail_type == 15)
                        $row->foster = $detail->detail_count;
                }
            }
            if($row->posts){
                foreach ($row->posts as $post){
                    switch ($post->request_type){
                        case 1:
                            $row->totalWechatRequest++;
                            break;
                        case 2:
                            $row->todayWechatRequest++;
                            break;
                        case 3:
                            $row->totalAlipayRequest++;
                            break;
                        default:
                            break;
                    }
                }
            }
            unset($row->deviceDetailed);
            unset($row->posts);
        }
        $header = [
            'ID号','所属分组','设备编号','手机号','微信','微信查询次数','微信添加次数','微信好友数',
            '支付宝查询次数','名片推送次数','被粉丝删除次数','通讯录添加次数','昨日涨粉量'];
        $this->exportExcel($devices,$header,$filename,'jiafen','');
    }
}
